package  
{
	import flash.accessibility.ISearchableText;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import net.flashpunk.Entity;
	import net.flashpunk.masks.Pixelmask;
	import net.flashpunk.utils.Input;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.Graphic;
	import net.flashpunk.FP;
	import flash.ui.Mouse;
	
	/**
	 * © 2012 TheJavaArcade.com
	 */	
	public class Paddle extends Entity {
		
		private var paddle_sprite:Image = new Image(Assets.PADDLE_SPRITE);
		private var paddle_mask:Pixelmask = new Pixelmask(Assets.PADDLE_SPRITE);
		
		public var state:int = 0;	//0=level 1=angle
		public var alreadyCollided:Boolean;
		
		public var x_velocity:int = 0;
		public var y_velocity:int = 0;
		public var x_destination:int;
		public var y_destination:int;
		
		public var initialPaddleMove:Boolean;
		public var mX:int;	//mouse X & Y coordinates
		public var mY:int;	//	used to determine when the mouse moves after launching the ball
		
		public function Paddle() {
			graphic = paddle_sprite;
			mask = paddle_mask;
			type = "paddle";
			Mouse.hide();
			layer = 0;
			x = Input.mouseX - 20;
			y = Input.mouseY - 20;
		}
		
		override public function update():void {
			if (GameWorld.ballOut) {
				if (mX != Input.mouseX || mY != Input.mouseY) {
					initialPaddleMove = true;
				}
			}
			x_destination = Input.mouseX - 20;
			y_destination = Input.mouseY - 20;
		//move the paddle
			x_velocity = 0;
			y_velocity = 0;
			if (x_destination - x < 10 && x_destination - x > -10) {
				if (x_destination < x) x_velocity = -1;
				if (x_destination > x) x_velocity = 1;
			}else {
				x_velocity = (x_destination - x) / 10;				
			}
			if (y_destination - y < 10 && y_destination - y > -10) {
				if (y_destination < y) y_velocity = -1;
				if (y_destination > y) y_velocity = 1;
			}else {
				y_velocity = (y_destination - y) / 10;				
			}
		//max speed
			if (x_velocity > 7) x_velocity = 7;
			if (x_velocity < -7) x_velocity = -7;
			if (y_velocity > 7) y_velocity = 7;
			if (y_velocity < -7) y_velocity = -7;
		//prevents paddle from moving until appropriate
			if (!GameWorld.isBonus && !GameWorld.gameOver &&  GameWorld.ballOut && initialPaddleMove) {
				x = x + x_velocity;
				y = y + y_velocity;
			}
		//paddle boundaries
			if (x < 40) {
				x = 40;
				x_velocity = 0;
			}
			if (x + paddle_sprite.width > FP.width - 40) {
				x = FP.width - 40 - paddle_sprite.width;
				x_velocity = 0;
			}
			if (y < 60) {
				y = 60;
				y_velocity = 0;
			}
			if (y + paddle_sprite.height > FP.height - 40) {
				y = FP.height - 40 - paddle_sprite.height;
				y_velocity = 0;
			}
		//ball collision
			if (collide("ball", x + x_velocity, y) && x_velocity != 0) {
				GameWorld.ball.x = GameWorld.ball.x + x_velocity;
				GameWorld.ball.x_velocity  = x_velocity + (1*(x_velocity/Math.abs(x_velocity)));
			}
			if (collide("ball", x, y + y_velocity) && y_velocity != 0) {
				GameWorld.ball.y = GameWorld.ball.y + y_velocity;
				GameWorld.ball.y_velocity = y_velocity + (1*(y_velocity/Math.abs(y_velocity)));
			}				
		}	
	}
	
}